//
//  XLPasswordVC.m
//  industry
//
//  Created by xl on 2020/12/28.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLPasswordVC.h"
#import "XLPasswordView.h"
#import "XLUserDefaults.h"

@interface XLPasswordVC ()<XLPasswordViewDelegate>

@property (nonatomic, strong) XLLabel *titleLB;
@property (nonatomic, strong) XLLabel *infosLB;
@property (nonatomic, strong) XLPasswordView *passwordView;
@property (nonatomic, strong) XLButton *sureBtn;

@property (nonatomic, copy) NSString *lastPS;
@property (nonatomic, copy) NSString *currentPS;

@property (nonatomic, assign) NSInteger nextStep;//如果是设置密码 1第一次设置密码 2再次确认密码；如果是更换密码 1是校验密码  2.设置新密码  3.再次确认密码


@end

@implementation XLPasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //设置默认初始值
    self.nextStep = 1;
    [self buildView];
    if (self.type == XLPasswordVCFromTypeSetting) {
        self.navigationItem.title = @"解锁设置";
        _infosLB.text = @"(请输入新的数字密码)";
    } else {
        self.navigationItem.title = @"修改解锁设置";
        _infosLB.text = @"(请输入之前设置的数字密码)";
    }
}

- (void)buildView {
    [self.view addSubview:self.titleLB];
    [self.view addSubview:self.infosLB];
    [self.view addSubview:self.passwordView];
    [self.view addSubview:self.sureBtn];
    
    [_titleLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.top.equalTo(self.view).offset(25);
    }];
    
    [_infosLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLB.mas_right).offset(10);
        make.centerY.equalTo(self.titleLB);
    }];
    
    [_passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.titleLB.mas_bottom).offset(30);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 60));
    }];
    
    [_sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLB);
        make.top.equalTo(self.passwordView.mas_bottom).offset(40);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 30*2, 45));
    }];
}

#pragma mark - UIButton
- (void)clickSure {
    if (ISEmptyString(self.lastPS) || ISEmptyString(self.currentPS)) {
        [XLToast showErrorMessage:@"当前设置数字密码不合法！"];
        return;
    }
    if (![self.lastPS isEqualToString:self.currentPS]) {
        [XLToast showErrorMessage:@"前后密码不一致！"];
        return;
    }
    [self.view endEditing:YES];
    [XLToast showSuccessMessage:@"设置成功"];
    //存储合理密码
    [XLUserDefaults saveLastNumPassword:self.currentPS];
    if (self.resultBlock) {
        self.resultBlock();
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - XLPasswordViewDelegate
- (void)passWordDidChange:(XLPasswordView *)password {
    NSLog(@"改变%@", password.textStore);
    if (password.textStore.length == 4) {
        if (self.type == XLPasswordVCFromTypeSetting) {
            if (self.nextStep == 1) {//第一次输入数字密码
                _infosLB.text = @"(请再次输入数字密码)";
                self.nextStep = 2;
                self.lastPS = password.textStore;
                [password clearCurrentPassword];
            } else {//第二次输入数字密码
                self.currentPS = password.textStore;
            }
        } else {
            NSString *savePS = [XLUserDefaults getLastNumPassword];
            if (self.nextStep == 1) {//校验密码
                if ([savePS isEqualToString:password.textStore]) {
                    self.nextStep = 2;
                    _infosLB.text = @"(请输入新的数字密码)";
                    [password clearCurrentPassword];
                } else {
                    _infosLB.text = @"（数字密码校验错误）";
                    [self shakeAnimationForView:self.infosLB];
                }

            } else if (self.nextStep == 2) {
                _infosLB.text = @"请再次输入新的数字密码";
                self.nextStep = 3;
                self.lastPS = password.textStore;
                [password clearCurrentPassword];
            } else {
                self.currentPS = password.textStore;
            }
        }
    }
}

-(void)passWordBeginInput:(XLPasswordView *)password
{
    NSLog(@"开始输入%@",password.textStore);
}

-(void)passWordCompleteInput:(XLPasswordView *)password
{
    NSLog(@"输入完成%@",password.textStore);
}

#pragma mark - Private
// 抖动动画
- (void)shakeAnimationForView:(UIView *)view {
    CALayer *viewLayer = view.layer;
    CGPoint position = viewLayer.position;
    CGPoint left = CGPointMake(position.x - 10, position.y);
    CGPoint right = CGPointMake(position.x + 10, position.y);
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [animation setFromValue:[NSValue valueWithCGPoint:left]];
    [animation setToValue:[NSValue valueWithCGPoint:right]];
    [animation setAutoreverses:YES]; // 平滑结束
    [animation setDuration:0.08];
    [animation setRepeatCount:3];
    
    [viewLayer addAnimation:animation forKey:nil];
}

#pragma mark - Setter/getter
- (XLLabel *)titleLB {
    if (!_titleLB) {
        _titleLB = [XLLabel new];
        _titleLB.font = [UIFont fontOfPingFangSCSemiboldWithSize:15];
        _titleLB.textColor = [UIColor blackFontColor];
        _titleLB.text = @"设置解锁密码";
    }
    return _titleLB;
}

- (XLLabel *)infosLB {
    if (!_infosLB) {
        _infosLB = [XLLabel new];
        _infosLB.font = [UIFont fontOfPingFangSCRegularWithSize:13];
        _infosLB.textColor = [UIColor blackFontColor];
        _infosLB.text = @"(非登录密码)";
    }
    return _infosLB;
}

- (XLPasswordView *)passwordView {
    if (!_passwordView) {
        _passwordView = [[XLPasswordView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 60)];
        _passwordView.backgroundColor = [UIColor whiteColor];
        _passwordView.delegate = self;
    }
    return _passwordView;
}

- (XLButton *)sureBtn {
    if (!_sureBtn) {
        _sureBtn = [XLButton new];
        _sureBtn.backgroundColor = [UIColor blueBgColor];
        _sureBtn.layer.cornerRadius = 22.5;
        _sureBtn.layer.masksToBounds = YES;
        _sureBtn.titleLabel.font = [UIFont fontOfPingFangSCSemiboldWithSize:16];
        [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(clickSure) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}
@end
